﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using FX.CC.Andon.Entity.ReportManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.ReportManage
{
    public class EventSummaryReptService : DbHelperBase
    {
        public IList<EventSummaryReptEntity> GetEventTimeList(EventSummaryReptQcEntity qcmodel)
        {
            var list = new List<EventSummaryReptEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append(" SELECT F.*,X.DetailName FROM ( ");
            strSql.Append(" Select T.DetailCode, SUM(T.TM-T.ReduceMin*60)/3600.00 TM From(   ");
            strSql.Append(" SELECT D.DetailCode,A.EqID,A.ETypeCode,A.DetailID,A.IsCancle,A.BizDate,A.StartTime,A.EndTime,A.TM,A.ReduceMin FROM ( ");
            strSql.Append(" SELECT EqID,DetailID,ETypeCode,IsCancle,BizDate,StartTime,EndTime,datediff(second,StartTime, EndTime) as TM,ReduceMin FROM UT_EventRec ) AS A   ");
            strSql.Append(" INNER JOIN UT_Equipment AS B ON A.EqID=B.EqID ");
            strSql.Append(" INNER JOIN UT_Line AS C ON B.LineID=C.LineID ");
            strSql.Append(" INNER JOIN UT_ETypeDetail AS D ON A.DetailID=D.DetailID ");
            strSql.Append(" WHERE A.EndTime IS NOT NULL AND A.IsCancle=0 ");
            if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
            {
                strSql.Append(" AND A.BizDate >=@SBizDate");
            }
            if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
            {
                strSql.Append(" AND A.BizDate <=@EBizDate");
            }
            if (!string.IsNullOrEmpty(qcmodel.EqID))
            {
                strSql.Append(" AND A.EqID=@EqID ");
            }
            if (!string.IsNullOrEmpty(qcmodel.LineID))
            {
                strSql.Append(" AND C.LineID =@LineID ");
            }
            if (!string.IsNullOrEmpty(qcmodel.ETypeCode))
            {
                strSql.Append(" AND A.ETypeCode =@ETypeCode ");
            }
            strSql.Append(" )AS T GROUP BY T.DetailCode ");
            strSql.Append(" ) AS F ");
            strSql.Append(" LEFT JOIN UT_ETypeDetail AS X ON F.DetailCode=X.DetailCode ");
            strSql.Append(" ORDER BY F.TM DESC ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
                    {
                        Db.AddInParameter(cmd, "@SBizDate", DbType.DateTime, Convert.ToDateTime(qcmodel.BizDateFrom));
                    }
                    if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
                    {
                        Db.AddInParameter(cmd, "@EBizDate", DbType.DateTime, Convert.ToDateTime(qcmodel.BizDateEnd));
                    }
                    if (!string.IsNullOrEmpty(qcmodel.EqID))
                    {
                        Db.AddInParameter(cmd, "@EqID", qcmodel.EqID);
                    }
                    if (!string.IsNullOrEmpty(qcmodel.LineID))
                    {
                        Db.AddInParameter(cmd, "@LineID", qcmodel.LineID);
                    }
                    if (!string.IsNullOrEmpty(qcmodel.ETypeCode))
                    {
                        Db.AddInParameter(cmd, "@ETypeCode", qcmodel.ETypeCode);
                    }
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new EventSummaryReptEntity
                            {
                                DetailName =
                                    reader["DetailName"] == obj
                                        ? null
                                        : Convert.ToString(reader["DetailName"])
                            };
                            if (reader["TM"] != DBNull.Value)
                                model.Tm = Math.Round((decimal)reader["TM"], 2);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }

        public IList<EventSummaryReptEntity> GetEventNumberList(EventSummaryReptQcEntity qcmodel)
        {
            var list = new List<EventSummaryReptEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append(" SELECT F.*,X.DetailName FROM ( ");
            strSql.Append(" Select T.DetailCode,COUNT(T.DetailCode) Number From(  ");
            strSql.Append(" SELECT D.DetailCode FROM UT_EventRec AS A   ");
            strSql.Append(" INNER JOIN UT_Equipment AS B ON A.EqID=B.EqID ");
            strSql.Append(" INNER JOIN UT_Line AS C ON B.LineID=C.LineID ");
            strSql.Append(" INNER JOIN UT_ETypeDetail AS D ON A.DetailID=D.DetailID ");
            strSql.Append(" WHERE A.EndTime IS NOT NULL AND A.IsCancle=0 ");
            if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
            {
                strSql.Append(" AND A.BizDate >=@SBizDate");
            }
            if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
            {
                strSql.Append(" AND A.BizDate <=@EBizDate");
            }
            if (!string.IsNullOrEmpty(qcmodel.EqID))
            {
                strSql.Append(" AND A.EqID=@EqID ");
            }
            if (!string.IsNullOrEmpty(qcmodel.LineID))
            {
                strSql.Append(" AND C.LineID =@LineID ");
            }
            if (!string.IsNullOrEmpty(qcmodel.ETypeCode))
            {
                strSql.Append(" AND A.ETypeCode =@ETypeCode ");
            }
            strSql.Append(" )AS T GROUP BY T.DetailCode ");
            strSql.Append(" ) AS F ");
            strSql.Append(" LEFT JOIN UT_ETypeDetail AS X ON F.DetailCode=X.DetailCode ");
            strSql.Append(" ORDER BY F.Number DESC ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
                    {
                        Db.AddInParameter(cmd, "@SBizDate", DbType.DateTime, Convert.ToDateTime(qcmodel.BizDateFrom));
                    }
                    if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
                    {
                        Db.AddInParameter(cmd, "@EBizDate", DbType.DateTime, Convert.ToDateTime(qcmodel.BizDateEnd));
                    }
                    if (!string.IsNullOrEmpty(qcmodel.EqID))
                    {
                        Db.AddInParameter(cmd, "@EqID", qcmodel.EqID);
                    }
                    if (!string.IsNullOrEmpty(qcmodel.LineID))
                    {
                        Db.AddInParameter(cmd, "@LineID", qcmodel.LineID);
                    }
                    if (!string.IsNullOrEmpty(qcmodel.ETypeCode))
                    {
                        Db.AddInParameter(cmd, "@ETypeCode", qcmodel.ETypeCode);
                    }
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new EventSummaryReptEntity
                            {
                                DetailName =
                                    reader["DetailName"] == obj
                                        ? null
                                        : Convert.ToString(reader["DetailName"])
                            };
                            if (reader["Number"] != DBNull.Value)
                                model.Number = Convert.ToInt32(reader["Number"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
    }
}
